import Vec3 from './Vec3';
import Renderer from './Renderer';
import Model from './Model';
import Scene from './Director';
import director from './Director';
import Quaternion from './Quaternion';



const te = {
    Vec3: Vec3,
    Renderer: Renderer,
    Model: Model,
    Scene: Scene,
}
window['start'] = function start() {
    // 启用深度测试
    let renderer = te.Renderer.instance;
    renderer.compliePrograms();
    let model1 = new te.Model('test1');
    model1.setPosition(1, 0, -5);
    model1.loadBuffers(renderer.gl, '../models/房屋.obj');
    model1.setProgram(renderer.getShader('lightShader'));
    let model2 = new te.Model('test2');
    model2.setPosition(-2, 0, -5);
    model2.loadBuffers(renderer.gl, '../models/战斧.obj');
    model2.setProgram(renderer.getShader('lightShader'));

    director.loadScene([model1, model2]);


    let isDragging = false;
    // 触摸事件处理
    let lastTouchX = 0;
    let lastTouchY = 0;
    let rotationX = 0;
    let rotationY = 0;
    let lastX = 0;
    let lastY = 0;
    window.addEventListener("touchstart", (e) => {
        lastTouchX = e.touches[0].clientX;
        lastTouchY = e.touches[0].clientY;
    });

    window.addEventListener("touchmove", (e) => {
        if (e.touches.length === 1) {
            const touch = e.touches[0];
            const deltaX = touch.clientX - lastTouchX;
            const deltaY = touch.clientY - lastTouchY;

            rotationX += deltaY * 0.01; // 控制旋转速度
            rotationY += deltaX * 0.01;

            lastTouchX = touch.clientX;
            lastTouchY = touch.clientY;
        }
    });

    window.addEventListener("contextmenu", (e) => {
        e.preventDefault();
    });
    // 鼠标事件处理
    window.addEventListener("mousedown", (e) => {
        e.preventDefault();
        isDragging = true;
        lastX = e.clientX;
        lastY = e.clientY;
    });

    window.addEventListener("mousemove", (e) => {
        if (isDragging) {
            const newX = e.clientX;
            const newY = e.clientY;
            const deltaX = -newX + lastX;
            const deltaY = -newY + lastY;
            // const deltaX = touch.clientX - lastTouchX;
            // const deltaY = touch.clientY - lastTouchY;

            // 计算旋转角度
            const rotationY = deltaX * 0.01; // 水平滑动对应Y轴旋转
            const rotationX = deltaY * 0.01; // 垂直滑动对应X轴旋转

            // 创建绕Y轴的四元数
            let yRotationQuat = Quaternion.fromAxisAngle(new Vec3(0, 1, 0), rotationY);

            // 创建绕X轴的四元数
            let xRotationQuat = Quaternion.fromAxisAngle(new Vec3(1, 0, 0), rotationX);

            if (e.buttons === 1) {
                console.log('左键')
                // 组合旋转
                model1.rotation.multiply(yRotationQuat); // 先绕Y轴旋转
                model1.rotation.multiply(xRotationQuat); // 再绕X轴旋转
            } else if (e.buttons === 2) {
                console.log('右键')

                let camera = Renderer.instance.camera;
                // 组合旋转
                camera.rotation.multiply(yRotationQuat); // 先绕Y轴旋转
                // camera.rotation = camera.rotation.multiply(xRotationQuat); // 再绕X轴旋转
                // Renderer.instance.camera.updateViewMatrix();

            }
            // 更新触摸点位置
            lastX = newX;
            lastY = newY;
        }
    });

    window.addEventListener("mouseup", () => {
        isDragging = false;
    });

    window.addEventListener("keydown", (e) => {
        console.log(e)
        let code = e.code;
        let move_dist = 0.05;
        if (code === "KeyW") {
            renderer.camera.position.z += move_dist;
        }
        else if (code === "KeyS") {
            renderer.camera.position.z -= move_dist;
        }
        else if (code === "KeyA") {
            renderer.camera.position.x -= move_dist;
        }
        else if (code === "KeyD") {
            renderer.camera.position.x += move_dist;
        }
        else if (code === "KeyQ") {
            renderer.camera.position.y -= move_dist;
        }
        else if (code === "KeyE") {
            renderer.camera.position.y += move_dist;
        }
        // console.log('cameraX', cameraX, 'cameraY', cameraY, 'cameraZ', cameraZ)
        // 更新视图矩阵
        // updateViewMatrix();
        // gl.uniformMatrix4fv(viewMatrixLocation, false, viewMatrix);
    });
}
window['start']()
//@ts-ignore
window.te = te;
